package com.httpClient.cookies;

import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * @author bigFish
 * @Title: public:
 */
public class MyCookiesForPost {
    private String url = null;
    private String uri = null;
    private ResourceBundle bundle;
    CookieStore cookieStore = new BasicCookieStore();

    @BeforeTest
    public void beFor() {
        //读取配置文件
        bundle = ResourceBundle.getBundle("application", Locale.CHINA);
        //根据key可以获取到value
        url = bundle.getString("test.url");
    }

    @Test
    public void testGetCookies() throws IOException {
        this.uri = bundle.getString("getCookies");
        //创建get协议请求，拼接地址
        HttpGet get = new HttpGet(url + uri);
        //构建client对象
        CloseableHttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
        //执行请求并获取结果
        HttpEntity entity = client.execute(get).getEntity();
        //转换类型
        String s = EntityUtils.toString(entity, "utf-8");
        //输出信息
        System.out.println(s);
        //获取cookies信息
        List<Cookie> cookiesList = cookieStore.getCookies();
        for (Cookie cookie : cookiesList) {
            String name = cookie.getName();
            String value = cookie.getValue();
            System.out.println("[" + name + "," + value + "]");
        }
    }

    @Test(dependsOnMethods = "testGetCookies")
    public void testPostCookies() throws IOException {
        this.uri = bundle.getString("postWithCookies");
        //创建post协议请求，拼接地址
        HttpPost post = new HttpPost(url + uri);
        //构建client对象，设置cookies
        CloseableHttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
        //设置请求头信息
        post.setHeader("count-type","application/json");
        //设置请求参数
        HashMap<String, String> map = new HashMap<>(16);
        map.put("keyWord", "withCookies");
        JSONObject json = new JSONObject();
        json.put("keyWord","withCookies");
        //将参数设置到请求方法中
        StringEntity entity = new StringEntity(String.valueOf(json),"utf-8");
        post.setEntity(entity);
        //发起请求
        HttpEntity entity1 = client.execute(post).getEntity();
        //获取响应结果
        String s = EntityUtils.toString(entity1);
        System.out.println(s);
        //处理响应结果,把结果转换为json对象
        JSONObject object =  JSONObject.fromObject(s);
        System.out.println(object);
        //判断结果是否符合预期
        Assert.assertEquals(object.get("code"),200);
        Assert.assertEquals(object.get("data"),"data");
    }
}
